package com.example.demo.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.entity.CarKeep;
import com.example.demo.entity.CarKeep2;
import com.example.demo.entity.CarNumberCompany1;
import com.example.demo.entity.CarsOrder;
import com.example.demo.entity.CarsOrder2;
import com.example.demo.entity.Insurance;
import com.example.demo.entity.MoneyCompany;
import com.example.demo.entity.NewCar1;
import com.example.demo.entity.NewCar2;
import com.example.demo.entity.SaleBoutique1;
import com.example.demo.entity.SaleBoutique2;
import com.example.demo.entity.ThreadInput2;
import com.example.demo.entity.UsedCar1;
import com.example.demo.entity.UsedCar2;
import com.example.demo.entity.XiMei;
import com.example.demo.entity.XiMei2;
import com.example.demo.entity.YanBao;
import com.example.demo.entity.YanBao2;
import com.example.demo.form.CarsOrderForm;
import com.example.demo.security.UserUtils;
import com.example.demo.service.CarKeep2Service;
import com.example.demo.service.CarKeepService;
import com.example.demo.service.CarNumberCompanyService1;
import com.example.demo.service.CarsOrder2Service;
import com.example.demo.service.CarsOrderService;
import com.example.demo.service.InsuranceService;
import com.example.demo.service.MoneyCompanyService;
import com.example.demo.service.NewCar1Service;
import com.example.demo.service.NewCar2Service;
import com.example.demo.service.SaleBoutique1Service;
import com.example.demo.service.SaleBoutique2Service;
import com.example.demo.service.SaleComputerService;
import com.example.demo.service.ThreadInput2Service;
import com.example.demo.service.ThreadInputService;
import com.example.demo.service.TwoCompany2Service;
import com.example.demo.service.TwoCompanyService;
import com.example.demo.service.UsedCar1Service;
import com.example.demo.service.UsedCar2Service;
import com.example.demo.service.XiMei2Service;
import com.example.demo.service.XiMeiService;
import com.example.demo.service.YanBao2Service;
import com.example.demo.service.YanBaoService;

@RestController
@RequestMapping(value = "/carsOrder")
public class CarsOrderController {
	/**
	 * 客户线索1
	 */
	@Autowired
	private ThreadInputService threadPutDAO;
	
	/**
	 * 新车1
	 */
	@Autowired
	private NewCar1Service  newcarService;
	
	@Autowired
	private SaleComputerService saleComputerService;
	
	@Autowired
	private UsedCar1Service  oldCarService;
	@Autowired
	private UsedCar2Service  oldCarService2;
	
	/**
	 * 新车2
	 */
	@Autowired
	private NewCar2Service  newcar2Service;
	
	/**
	 * 客户线索2
	 */
	@Autowired
	private ThreadInput2Service thread2PutDAO;
	
	
	@Autowired
	private CarsOrder2Service carsOrder2DAO;
	
	@Autowired
	private CarsOrderService carsOrderDAO;
	
	@Autowired 
	private SaleBoutique1Service saleBoutique1DAO;
	
	@Autowired 
	private SaleBoutique2Service saleBoutique2DAO;
	
	@Autowired
	private CarKeepService carKeepService1;
	@Autowired
	private CarKeep2Service  carKeepService2;
	
	
	@Autowired
	private YanBao2Service yanBaoService2;
	@Autowired
	private YanBaoService yanBaoService1;
	
	@Autowired
	private XiMei2Service xiMeiService2;
	@Autowired
	private XiMeiService xiMeiService1;
	
	@Autowired
	private InsuranceService insuranceDAO;
	
	@Autowired
	private MoneyCompanyService MoneyCompanyService;
	
	@Autowired
	private CarNumberCompanyService1 carnumService;
	
	
	@Autowired
	private TwoCompanyService TwoCompanyService1;
	
	@Autowired
	private TwoCompany2Service TwoCompanyService2;
	
	@Autowired
	
	private UserUtils userUtils;
	
	
	@RequestMapping(value = "/phone")
	public Object findByPhone(String phone) throws ParseException {
		if(thread2PutDAO.findByPhoneAndThreadUserId(phone,userUtils.getUser()).size()>0) {
			
			List<ThreadInput2> list= thread2PutDAO.findByPhoneAndThreadUserId(phone,userUtils.getUser());
			ThreadInput2 yan=new ThreadInput2();
			Integer id=0;
			for(ThreadInput2 y :list) {
				if(id<y.getId()) {
					id=y.getId();
					yan=y;
				}
			}
			return new AjaxResult(yan);
		}else {
			 return new AjaxResult(false, "没有找到数据");
		}
	}
	
	@RequestMapping(value = "/xiuPhone")
	public Object findByXiuPhone(String phone) throws ParseException {
		if(phone.length()==11) {
			CarsOrder model = carsOrderDAO.findByPhoneAndUserId(phone,userUtils.getUser());
			if(model==null) {
				return new AjaxResult(false, "数据为空");
			}
			List<CarsOrder2> list = carsOrder2DAO.findByCarsorder(model);
			CarsOrder2 d=new CarsOrder2();
			Integer id=0;
			for(CarsOrder2 y :list) {
				if(id<y.getId()) {
					id=y.getId();
					d=y;
				}
			}
			
			return new AjaxResult(d);
			
		}else {
			 return new AjaxResult(false, "电话号码不正确");
		}
	}
	
	
	
	
	@RequestMapping(value = "/save")
	public Object save(CarsOrderForm form) {
		Date date=new Date();
		Integer cars1Id= form.getId();
		CarsOrder cars1= new CarsOrder();
		CarsOrder Cars1= new CarsOrder();
		CarsOrder2 cars2= new CarsOrder2();
		CarsOrder2 Cars2= new CarsOrder2();
		Float zeng=(float)0;
		Float xiao=(float)0;
		if(cars1Id==null) {
			BeanUtils.copyProperties(form, Cars1, "id","userId");
			Cars1.setUserId(userUtils.getUser());
			cars1 = carsOrderDAO.save(Cars1);
			if(form.getCars()!=null) {
				List<NewCar1> cars = form.getCars();
				for (NewCar1 newCar1 : cars) {
					List<NewCar2> newCar2 = newcar2Service.findByNewCar(newCar1);
					for (NewCar2 newCar22 : newCar2) {
						if(newCar22.getMany()>0) {
							NewCar2 newcar222=new NewCar2();
							newcar222.setMany(newCar22.getMany()-1);	
							newcar222.setXiuGaiDate(date);
							newcar222.setTiCheMoney(newCar22.getTiCheMoney());
							newcar222.setDate(newCar22.getDate());
							newcar222.setType(newCar22.getType());
							newcar222.setFanLiPrice(newCar22.getFanLiPrice());
							newcar222.setZhiDaoPrice(newCar22.getZhiDaoPrice());
							newcar222.setZhiHuanSubsidy(newCar22.getZhiHuanSubsidy());
							newcar222.setNewCar(newCar1);
							newcar2Service.save(newcar222);
						}else {
							return new AjaxResult(false,"汽车库存不足！");
						}
					}
				}
			}
			if(form.getCars2()!=null) {
				List<UsedCar1> cars = form.getCars2();
				for (UsedCar1 UsedCar1 : cars) {
					List<UsedCar2> UsedCar2 = oldCarService2.findByUsedCar(UsedCar1);
					for (UsedCar2 UsedCar22 : UsedCar2) {
						if(UsedCar22.getShuliang()>0) {
							UsedCar2 UsedCar222=new UsedCar2();
							UsedCar22.setEndDays(date);
							UsedCar222.setShuliang(UsedCar22.getShuliang()-1);	
							UsedCar222.setStartDays(date);
							UsedCar222.setSgjg(UsedCar22.getSgjg());
							UsedCar222.setType(UsedCar22.getType());
							UsedCar222.setUsedCar(UsedCar1);
							oldCarService2.save(UsedCar222);
							oldCarService2.save(UsedCar22);
						}else {
							return new AjaxResult(false,"汽车库存不足！");
						}
					}
				}
			}
		}else {
			Cars1 = carsOrderDAO.findById(cars1Id);
			BeanUtils.copyProperties(form, Cars1, "id","userId");
			cars1 = carsOrderDAO.save(Cars1);
			
			List<CarsOrder2> list=carsOrder2DAO.findByCarsorder(Cars1);
			cars2=new CarsOrder2();
			Integer id=0;
			for(CarsOrder2 y :list){
				if(id<y.getId()) {
					id=y.getId();
					cars2=y;
				}
			}
			cars2.setEndDate(date);
			carsOrder2DAO.save(cars2);	  	
			
		}
		
		//赠精品
		 if(form.getZengJingPin()!=null&&form.getZengJingPin().length()>0){
	         	List<SaleBoutique1> model = new ArrayList<>();
	         	String model2=form.getZengJingPin();
	         	for (String name : model2.split(",")) {
	         		model.add(saleBoutique1DAO.findById(Integer.parseInt(name)));
	         		List<SaleBoutique2> list = saleBoutique2DAO.findBySaleBoutique(saleBoutique1DAO.findById(Integer.parseInt(name)));
	         		SaleBoutique2 d=new SaleBoutique2();
	    			Integer id=0;
	    			for(SaleBoutique2 y :list) {
	    				if(id<y.getId()) {
	    					id=y.getId();
	    					d=y;
	    				}
	    			}
	    			zeng+=d.getCaiLiaoPrice()+d.getGongShiPrice();
	         	}
	         Cars2.setZengJingPin(model);
	      }
		 
		//赠保养
		 if(form.getZengBaoYang()!=null&&form.getZengBaoYang().length()>0){
	         	List<CarKeep> model = new ArrayList<>();
	         	String model2=form.getZengBaoYang();
	         	for (String name : model2.split(",")) {
	         		model.add(carKeepService1.findById(Integer.parseInt(name)));
	         		List<CarKeep2> list = carKeepService2.findByCarKeep(carKeepService1.findById(Integer.parseInt(name)));
	         		CarKeep2 d=new CarKeep2();
	    			Integer id=0;
	    			for(CarKeep2 y :list) {
	    				if(id<y.getId()) {
	    					id=y.getId();
	    					d=y;
	    				}
	    			}
	    			if(d.getPrice()==null) {
	    				continue;
	    			}
	    			zeng+=d.getPrice();
	         	}
	         Cars2.setZengBaoYang(model);
	      }
			
		 
		 //赠延保
		 if(form.getZengYanBao()!=null&&form.getZengYanBao().length()>0){
	         	List<YanBao> model = new ArrayList<>();
	         	String model2=form.getZengYanBao();
	         	for (String name : model2.split(",")) {
	         		model.add(yanBaoService1.findById(Integer.parseInt(name)));
	         		List<YanBao2> list = yanBaoService2.findByYanBaoId(Integer.parseInt(name));
	         		YanBao2 d=new YanBao2();
	    			Integer id=0;
	    			for(YanBao2 y :list) {
	    				if(id<y.getId()) {
	    					id=y.getId();
	    					d=y;
	    				}
	    			}
	    			zeng+=d.getYanBaoPrice();
	         	}
	         Cars2.setZengYanBao(model);
	      }
		 
		 //赠洗美
		 if(form.getZengXiMei()!=null&&form.getZengXiMei().length()>0){
	         	List<XiMei> model = new ArrayList<>();
	         	String model2=form.getZengXiMei();
	         	for (String name : model2.split(",")) {
	         		model.add(xiMeiService1.findById(Integer.parseInt(name)));
	         		List<XiMei2> list = xiMeiService2.findByXiMeiId(Integer.parseInt(name));
	         		XiMei2 d=new XiMei2();
	    			Integer id=0;
	    			for(XiMei2 y :list) {
	    				if(id<y.getId()) {
	    					id=y.getId();
	    					d=y;
	    				}
	    			}
	    			zeng+=d.getCaiLiaoPrice()+d.getGongShiPrice();
	         	}
	         Cars2.setZengXiMei(model);
	      }
		 
		 if(form.getZengBaoXian()!=null&&form.getZengBaoXian().length()>0) {
			 List<Insurance> model = new ArrayList<>();
			 model.add(insuranceDAO.findById(Integer.parseInt(form.getZengBaoXian()))) ;
			 Cars2.setZengBaoXian(model);
		 }
		 
		 if(form.getZengJinRong()!=null&&form.getZengJinRong().length()>0) {
			 List<MoneyCompany> model = new ArrayList<>();
			 model.add(MoneyCompanyService.findById(Integer.parseInt(form.getZengJinRong()))) ;
			 Cars2.setZengJinRong(model);
		 }	 
		 if(form.getZengChePai()!=null&&form.getZengChePai().length()>0) {
			 List<CarNumberCompany1> model = new ArrayList<>();
			 model.add(carnumService.findById(Integer.parseInt(form.getZengChePai()))) ;
			 Cars2.setZengChePai(model);
		 }	 
		 
		 zeng+=form.getZengJiaoQiang();
		 zeng+=form.getZengShangYeXian();
		 zeng+=form.getZengSongJinRongMoney();
		 zeng+=form.getZengSongChePai();
		 Integer zeng1 = zeng.intValue();
		 Cars2.setZengPrice(zeng1);
		 
		 
		//-------------------------------------//
		 //售精品
		 if(form.getMaiJingPin()!=null&&form.getMaiJingPin().length()>0){
	         	List<SaleBoutique1> model = new ArrayList<>();
	         	String model2=form.getMaiJingPin();
	         	for (String name : model2.split(",")) {
	         		model.add(saleBoutique1DAO.findById(Integer.parseInt(name)));
	         		List<SaleBoutique2> list = saleBoutique2DAO.findBySaleBoutique(saleBoutique1DAO.findById(Integer.parseInt(name)));
	         		SaleBoutique2 d=new SaleBoutique2();
	    			Integer id=0;
	    			for(SaleBoutique2 y :list) {
	    				if(id<y.getId()) {
	    					id=y.getId();
	    					d=y;
	    				}
	    			}
	    			xiao+=d.getCaiLiaoPrice()+d.getGongShiPrice();
	         	}
	         Cars2.setMaiJingPin(model);
	      }
		 
		//售保养
		 if(form.getMaiBaoYang()!=null&&form.getMaiBaoYang().length()>0){
	         	List<CarKeep> model = new ArrayList<>();
	         	String model2=form.getMaiBaoYang();
	         	for (String name : model2.split(",")) {
	         		model.add(carKeepService1.findById(Integer.parseInt(name)));
	         		List<CarKeep2> list = carKeepService2.findByCarKeep(carKeepService1.findById(Integer.parseInt(name)));
	         		CarKeep2 d=new CarKeep2();
	    			Integer id=0;
	    			for(CarKeep2 y :list) {
	    				if(id<y.getId()) {
	    					id=y.getId();
	    					d=y;
	    				}
	    			}
	    			if(d.getPrice()==null) {
	    				continue;
	    			}
	    			xiao+=d.getPrice();
	         	}
	         Cars2.setMaiBaoYang(model);
	      }
		 
		 //赠延保
		 if(form.getMaiYanBao()!=null&&form.getMaiYanBao().length()>0){
	         	List<YanBao> model = new ArrayList<>();
	         	String model2=form.getMaiYanBao();
	         	for (String name : model2.split(",")) {
	         		model.add(yanBaoService1.findById(Integer.parseInt(name)));
	         		List<YanBao2> list = yanBaoService2.findByYanBaoId(Integer.parseInt(name));
	         		YanBao2 d=new YanBao2();
	    			Integer id=0;
	    			for(YanBao2 y :list) {
	    				if(id<y.getId()) {
	    					id=y.getId();
	    					d=y;
	    				}
	    			}
	    			xiao+=d.getYanBaoPrice();
	         	}
	         Cars2.setMaiYanBao(model);
	      }
		 
		 //赠洗美
		 if(form.getMaiXiMei()!=null&&form.getMaiXiMei().length()>0){
	         	List<XiMei> model = new ArrayList<>();
	         	String model2=form.getMaiXiMei();
	         	for (String name : model2.split(",")) {
	         		model.add(xiMeiService1.findById(Integer.parseInt(name)));
	         		List<XiMei2> list = xiMeiService2.findByXiMeiId(Integer.parseInt(name));
	         		XiMei2 d=new XiMei2();
	    			Integer id=0;
	    			for(XiMei2 y :list) {
	    				if(id<y.getId()) {
	    					id=y.getId();
	    					d=y;
	    				}
	    			}
	    			xiao+=d.getCaiLiaoPrice()+d.getGongShiPrice();
	         	}
	         Cars2.setMaiXiMei(model);
	      }
		 
		 if(form.getMaiBaoXian()!=null&&form.getMaiBaoXian().length()>0) {
			 List<Insurance> model = new ArrayList<>();
			 model.add(insuranceDAO.findById(Integer.parseInt(form.getMaiBaoXian()))) ;
			 Cars2.setMaiBaoXian(model);
		 }
		 
		 if(form.getMaiJinRong()!=null&&form.getMaiJinRong().length()>0) {
			 List<MoneyCompany> model = new ArrayList<>();
			 model.add(MoneyCompanyService.findById(Integer.parseInt(form.getMaiJinRong()))) ;
			 Cars2.setMaiJinRong(model);
		 }	 
		 if(form.getMaiChePai()!=null&&form.getMaiChePai().length()>0) {
			 List<CarNumberCompany1> model = new ArrayList<>();
			 model.add(carnumService.findById(Integer.parseInt(form.getMaiChePai()))) ;
			 Cars2.setMaiChePai(model);
		 }
		 
		 xiao+=form.getMaiJiaoQiang();
		 xiao+=form.getMaiShangYeXian();
		 xiao+=form.getXiaoShouJinRongMoney();
		 xiao+=form.getXiaoShouChePai();
		 Integer xiao1 = xiao.intValue();
		 Cars2.setMaiPrice(xiao1); 
		 
		 
		 
		BeanUtils.copyProperties(form, Cars2, "id","zengJingPin","zengBaoYang","zengYanBao","zengXiMei","zengBaoXian","zengJinRong","zengPrice","maiJingPin","maiBaoYang","maiYanBao","maiXiMei","maiBaoXian","maiJinRong","maiPrice");
		Cars2.setBeginDate(date);
		Cars2.setCarsorder(cars1);
		carsOrder2DAO.save(Cars2);
		return new AjaxResult("保存成功!");
	}
	
	
	/**
	 * @return 根据员工部门获取车型
	 */
	@RequestMapping(value = "/carType")
	public Object CarsType() {
		String deptName = userUtils.getUser().getDeptName();
		
		if("二手车部".equals(deptName)) {
			return new AjaxResult(oldCarService.findAll());
		}else {
			return new AjaxResult(newcarService.findAll());
		}
	}
	
	
}
